﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetOpenAuth.ApplicationBlock;
using PaymentBLL;

public partial class pages_SignInWithTwitter : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (TwitterConsumer.IsTwitterConsumerConfigured)
        {
            this.MultiView1.ActiveViewIndex = 1;

            if (!IsPostBack)
            {
                string screenName;
                int userId;
                if (TwitterConsumer.TryFinishSignInWithTwitter(out screenName, out userId))
                {
                    this.loggedInPanel.Visible = true;
                    this.loggedInName.Text = screenName;

                    // In a real app, the Twitter username would likely be used
                    // to log the user into the application.
                    //FormsAuthentication.RedirectFromLoginPage(screenName, false);
                    var ctr = new CtrAccount();
                    var info = ctr.SelectAccountInfoByName(userId.ToString());
                    if (info == null)
                    {
                        int? id = 0;
                        if(ctr.InsertAccount(userId.ToString(), screenName, 5, screenName
                                              , "", 1, ref id)<=0)
                            return;
                        if (id > 0)
                        {
                            AccountSession.Login(id.Value, userId.ToString(), screenName,5);
                        }
                        else
                        {
                            AccountSession.Logout();
                        }
                    }
                    else
                    {
                        if (info.Status > 0)
                        {
                            AccountSession.Login(info.Id, info.Name, info.FullName, info.TypeId);
                        }
                        else
                        {
                            AccountSession.Logout();
                        }
                    }
                }
            }
        }
    }
    protected void signInButton_Click(object sender, ImageClickEventArgs e)
    {
        TwitterConsumer.StartSignInWithTwitter(this.forceLoginCheckbox.Checked).Send();
    }
}